﻿using System.Collections.Generic;
using System.Text.RegularExpressions;
using Jayden.Dll.Core.Membership;
using Jayden.Dll.Web.Enums;
using Jayden.Dll.Web.HtmlControls;
using Jayden.Dll.Web.Json;

namespace Jayden.Dll.Web.Controls.Admin
{
	public class UserProviderTable : DataTable<User>
	{

	}

	public class UserProviderDiv : Div
	{
		private Input m_Term;
		private Button m_Search;
		private Input m_Email;
		private Input m_Username;
		private Input m_FirstName;
		private Input m_LastName;
		private SearchForm m_SearchForm;

		private UserProviderTable m_Results;

		public override void Load(System.Xml.XmlNode node, HtmlControlLoader loader)
		{
			base.Load(node, loader);

			m_SearchForm = GetElementByServerId<SearchForm>("searchForm", true);

			m_Term = GetElementByServerId<Input>("term", true, true);
			m_Search = GetElementByServerId<Button>("search", true, "command");
			m_Email = GetElementByServerId<Input>("email", true, true);
			m_Username = GetElementByServerId<Input>("username", true, true);
			m_FirstName = GetElementByServerId<Input>("firstName", true, true);
			m_LastName = GetElementByServerId<Input>("lastName", true, true);

			m_Results = GetElementByServerId<UserProviderTable>("results", true);

			m_Results.AddColumn("UserName", 120, delegate(User value, UserProviderTable.Column column, int index) { return DataTable.ParseText(value.UserName); }, null);
			m_Results.AddColumn("Email", 150, delegate(User value, UserProviderTable.Column column, int index) { return DataTable.ParseText(value.Email); }, null);
			m_Results.AddColumn("FirstName", 150, delegate(User value, UserProviderTable.Column column, int index) { return DataTable.ParseText(value.FirstName); }, null);
			m_Results.AddColumn("LastName", 150, delegate(User value, UserProviderTable.Column column, int index) { return DataTable.ParseText(value.LastName); }, null);
			m_Results.AddColumn("Creation", 100, delegate(User value, UserProviderTable.Column column, int index) { return DataTable.ParseText(value.CreationDate.ToShortDateString()); }, null);
			m_Results.AddColumn("Activity", 180, delegate(User value, UserProviderTable.Column column, int index) { return DataTable.ParseText(value.LastActivityDate.ToShortDateString() + " " + value.LastActivityDate.ToShortTimeString()); }, null);
		}

		public override void ProcessJsonRequest(HttpHandlerContext context)
		{
			base.ProcessJsonRequest(context);
			SearchForm.SearchCommandHandler searchHandler = context.HandlerManager.GetHandler<SearchForm.SearchCommandHandler>();
			searchHandler.Search += new SearchForm.OnSearch(OnSearch);
		}

		public override void ProcessHtmlRequest(HttpHandlerContext context)
		{
			base.ProcessHtmlRequest(context);
			m_SearchForm.FindLabel(m_Email);
			m_SearchForm.FindLabel(m_Username);
			m_SearchForm.FindLabel(m_FirstName);
			m_SearchForm.FindLabel(m_LastName);
			SearchForm.SearchCommandHandler searchHandler = context.HandlerManager.GetHandler<SearchForm.SearchCommandHandler>();
			searchHandler.Search += new SearchForm.OnSearch(OnSearch);
		}

		void OnSearch(HttpHandlerContext context, SearchForm.SearchValues search)
		{
			UserDataAccess userDataAccess = context.UnitOfWork.DataAccessManager.GetDataAccess<User>() as UserDataAccess;
			if (userDataAccess != null)
			{
				bool username = context.Request["username"] == "on";
				bool email = context.Request["email"] == "on";
				bool firstName = context.Request["firstName"] == "on";
				bool lastName = context.Request["lastName"] == "on";
				int count;
				List<User> users = userDataAccess.Search("%" + search.Term + "%", username, email, firstName, lastName, 0, int.MaxValue, out count);

				m_Results.Clear();
				m_Results.AddHeaders();
				foreach(User user in users)
					m_Results.AddTr(user).MarkText(new Regex(search.Term), null);

				if (context.RequestType == HttpRequestType.Json)
				{
					JsonObject results = new JsonObject();
					results.Add("html", m_Results.ToInnerHtml(context.Translator));
					context.Json.Add(m_Results.Id, results);
				}
			}
		}

		public override void ProcessJsRequest(HttpHandlerContext context)
		{
			base.ProcessJsRequest(context);
			string id = m_SearchForm.Id + "<" + GetType().FullName + ">";
			if (!context.Js.Contains(id))
			{
				context.Js[id].AppendAjaxHtmlHandler(m_Results, true, false);
				context.Js[id].AppendOnInit(@"
	var $form = $('#" + m_SearchForm.Id + @"');
	var $search = $('#" + m_Search.Id + @"');
	var submit = function () {
		return JAYDEN.sfn.submitAjax($form, $search);
	};
	$('#" + m_Email.Id + @"').click(submit);
	$('#" + m_Username.Id + @"').click(submit);
	$('#" + m_FirstName.Id + @"').click(submit);
	$('#" + m_LastName.Id + @"').click(submit);
	$form.submit(function(e) {
		e.preventDefault();
		return JAYDEN.sfn.submitAjax(this, $search);
	});
");
			}
		}
	}
}

